package com.xlh.controller.cases;

import com.xlh.common.Result;
import com.xlh.dto.PageDTO;
import com.xlh.dto.cases.*;
import com.xlh.enums.cases.JudgeEnum;
import com.xlh.param.cases.BusinessParam;
import com.xlh.param.cases.CreateCaseLibraryParam;
import com.xlh.param.cases.UpdateBusinessParam;
import com.xlh.param.cases.UpdateCaseLibraryParam;
import com.xlh.pojo.cases.Star;
import com.xlh.service.cases.BusinessCaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 行业案例
 * <p>
 * Created by lx on 2019/1/25.
 */
@Api(tags = "BusinessCaseController", description = "行业案例")
@RestController
@RequestMapping("/business/case")
public class BusinessCaseController {

    @Autowired
    private BusinessCaseService businessCaseService;

    @ApiOperation("行业案例列表")
    @GetMapping("/businesscaseList")
    @PreAuthorize("permitAll()")
    public Result<List<BusinessDTO>> listBusiness() {
        return Result.success(businessCaseService.listBusiness());
    }

    @ApiOperation("添加行业案例")
    @PostMapping("/addBusinessCase")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result createBusiness(@RequestBody @ApiParam(value = "创建参数") @Valid BusinessParam createParam) {
        businessCaseService.createBusiness(createParam);
        return Result.success();
    }

    @ApiOperation("修改行业案例修改信息")
    @PostMapping("/saveBusinessCase")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateBusiness(@RequestBody @ApiParam(value = "修改参数") @Valid UpdateBusinessParam updateParam) {
        businessCaseService.updateBusiness(updateParam);
        return Result.success();
    }

    @ApiOperation("删除行业及案例信息")
    @GetMapping("/delBusinessCaseById")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result delBusinessCase(@RequestParam(value = "businessCaseId") @ApiParam(value = "行业id") Long businessId) {
        businessCaseService.delBusinessCase(businessId);
        return Result.success();
    }

    @ApiOperation("案例列表")
    @GetMapping("/caseList")
    @PreAuthorize("permitAll()")
    public Result<List<CaseLibraryDTO>> getCaseList(@RequestParam(value = "businessCaseId")
                                                    @ApiParam(value = "行业id") Long businessId) {
        return Result.success(businessCaseService.listCase(businessId));
    }

    @ApiOperation("创建案例")
    @PostMapping("/makeCase")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result createCaseLibrary(@RequestBody @ApiParam(value = "创建案例参数") @Valid CreateCaseLibraryParam createParam) {
        businessCaseService.createCaseLibrary(createParam);
        return Result.success();
    }

    @ApiOperation("修改案例")
    @PostMapping("/saveCase")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result updateCaseLibrary(@RequestBody @ApiParam(value = "修改案例参数") @Valid UpdateCaseLibraryParam updateParam) {
        businessCaseService.updateCaseLibrary(updateParam);
        return Result.success();
    }

    @ApiOperation("案例详情")
    @GetMapping()
    @PreAuthorize("permitAll()")
    public Result<CaseLibraryDetailDTO> getCase(@RequestParam(value = "caseId") @ApiParam(value = "案例id") Long id) {
        return Result.success(businessCaseService.getCase(id));
    }

    @ApiOperation("删除案例")
    @GetMapping("/delCaseById")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result deleteCase(@RequestParam List<Long> caseIds) {
        businessCaseService.deleteCase(caseIds);
        return Result.success();
    }

    @ApiOperation("图片转换")
    @PostMapping("/changePic")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<List<CasePicDTO>> changePic(@RequestBody List<String> base64List) {
        return Result.success(businessCaseService.changePic(base64List));
    }

    @ApiOperation("点赞/取消点赞")
    @GetMapping("/clickStar")
    @PreAuthorize("@permissionValidator.isTeacherOrStudent()")
    public Result clickStar(@RequestParam @ApiParam(value = "是否点赞 0：点赞 1：取消点赞") Integer judge,
                            @RequestParam @ApiParam(value = "案例id") Long caseId) {
        businessCaseService.clickStar(JudgeEnum.getByCode(judge), caseId);
        return Result.success();
    }

    @ApiOperation("获取当前人是否对当前案例点赞")
    @GetMapping("/getStarInfoById")
    @PreAuthorize("permitAll()")
    public Result<List<Star>> getStarInfoById(@RequestParam @ApiParam(value = "案例id") Long caseId) {
        return Result.success(businessCaseService.getStarInfoById(caseId));
    }

    @ApiOperation("点赞排行")
    @GetMapping("/starRankList")
    @PreAuthorize("@permissionValidator.isAdmin()")
    public Result<PageDTO<StarRankDTO>> starRankList(@RequestParam(required = false, value = "pageNum", defaultValue = "1")
                                                     @ApiParam(value = "当前页") Integer pageNum,
                                                     @RequestParam(required = false, value = "pageSize", defaultValue = "10")
                                                     @ApiParam(value = "每页显示条数") Integer pageSize,
                                                     @RequestParam(required = false, value = "orderStatus", defaultValue = "0")
                                                     @ApiParam(value = "排序状态 0:默认按照id排序 1：教师点赞数正序  2：教师点赞数倒序 3：学生点赞数正序 4：学生点赞数倒序") Integer orderStatus) {
        return Result.success(businessCaseService.starRankList(pageNum, pageSize, orderStatus));
    }


}
